package cn.ipenta.auth.controller;

import cn.ipenta.config.Constants;
import cn.ipenta.auth.entity.TokenModel;
import cn.ipenta.auth.entity.User;
import cn.ipenta.auth.manager.TokenManager;
import cn.ipenta.auth.repository.UserRepository;
import cn.ipenta.config.ResultStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author ipenta
 * @ClassName: AuthController
 * @Description: TODO
 * @date 2017-09-25 下午10:30
 */
@ApiIgnore
@Controller
public class AuthController {

	@Autowired
	private UserRepository userRepository;

	@Autowired
	@Qualifier("defaultTokenManager")
	private TokenManager tokenManager;

	@GetMapping("/auth/signin")
	public String signin() {
		return "auth/signin";
	}

	@PostMapping("/auth/signin")
	public String doSignin(@RequestParam String email, @RequestParam String password, HttpServletRequest request, final RedirectAttributes redirectAttributes) {

		Assert.notNull(email, "email can not be empty");
		Assert.notNull(password, "password can not be empty");

		User user = userRepository.findByUsername(email);
		if (null == user ||  //未注册
				!user.getPassword().equals(password)) {  //密码错误
			redirectAttributes.addFlashAttribute("message", ResultStatus.USERNAME_OR_PASSWORD_ERROR.getMessage());
			return "redirect:/auth/signin";
		}
		//生成一个token，保存用户登录状态
		TokenModel model = tokenManager.createToken(user.getId());
		HttpSession session = request.getSession();
		session.setAttribute(Constants.CURRENT_USER, user.getUsername());
		session.setAttribute(Constants.SESSION_TOKEN, model);
		return "redirect:/home";
	}

	@GetMapping("/auth/signout")
	public String signout(HttpServletRequest request) {
		request.getSession().removeAttribute(Constants.SESSION_TOKEN);
		return "redirect:/auth/signin";

	}

	@GetMapping("/auth/signup")
	public String signup() {
		return "auth/signup";
	}

	@PostMapping("/auth/signup")
	public String doSignup(@RequestParam String email, @RequestParam String password, @RequestParam String review_password, HttpServletRequest request, final RedirectAttributes redirectAttributes) {

		if (!password.equals(review_password)) {
			//密码不匹配
			redirectAttributes.addFlashAttribute("message", ResultStatus.PASSWORD_UNCHECK.getMessage());
			return "redirect:/auth/signup";
		}

		User user = userRepository.findByUsername(email);

		if (null != user) {
			//提示用户名占用
			redirectAttributes.addFlashAttribute("message", ResultStatus.USERNAME_REGISTERED.getMessage());
			return "redirect:/auth/signup";
		}
		User userd = userRepository.save(new User(email, password));
		if (null == userd) {
			redirectAttributes.addFlashAttribute("message", ResultStatus.USER_REGISTER_FAIL.getMessage());
			return "redirect:/auth/signup";
		}
		TokenModel model = tokenManager.createToken(userd.getId());

		request.getSession().setAttribute("tokenModel", model);

		return "redirect:/home";
	}
}
